<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mmanager extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
	public function getListUser($number = 0,$offset = 0)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT * FROM user";
        
        $sql .= " ORDER BY created DESC";
        $count = $this->db->query($sql)->num_rows();
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function addUser($data)
    {//echo "<pre>"; die(print_r($data));
        $check = $this->db->query("SELECT id FROM user WHERE username = '".$data['username']."'")->num_rows();
        
        if($check == 0){
            $this->db->insert('user', $data);
            return 200;
        }
        
        return 201;
    }
    
    public function getUserInfo($id)
    {
        return $this->db->query("SELECT *,DATE_FORMAT(dateofbirth,'%d-%m-%Y') AS dateofbirth FROM user WHERE id = ".$id)->row_object(); 
    }
    
    public function editUser($id, $data)
    {
        $this->db->update('user', $data, "id = ".$id);
        
        return 200;
    }
    
    public function deleteUser($id)
    {
        $this->db->delete('user', "id = ".$id);
        
        return 200;
    }
    
    public function sendMail($data)
    {
        $config = array(
						'protocol' => 'smtp',
						'smtp_host' => $this->_smtp_host,
						'smtp_user' => $this->_smtp_user,
						'smtp_pass' => $this->_smtp_pass,
						'mailtype' => 'html'
                    );
        $this->load->library('email');
        $this->email->initialize($config);
        $body= "Bạn mới được tạo tài khoản từ trang quản lí project của Wicity "."<br>";
        $body.= "Tên tài khoản: ".$data['username']."<br>";
        $body.= "Password: ".$this->getParamString('password')."<br>";
        $body.= "Link đăng nhập: "."<a href='".site_url()."'>".site_url()."</a>"."<br>";
     
        $this->email->from($this->_smtp_user, 'Wicity');
        $this->email->to($data['email']);  
        $this->email->subject("Bạn đã được tạo 1 tài khoản từ Wicity project");
        $this->email->message($body);    
        
        $a=$this->email->send();
    }
}
